import json
import math

class PathAnalyzer:
    def __init__(self, path_file="path_points.json"):
        with open(path_file, 'r') as f:
            data = json.load(f)
            self.path_points = data["path_points"]
        
        # 计算总路径长度
        self.total_length = 0.0
        for i in range(1, len(self.path_points)):
            x1, y1 = self.path_points[i-1]
            x2, y2 = self.path_points[i]
            self.total_length += math.sqrt((x2-x1)**2 + (y2-y1)**2)
        
        print(f"路径总长度: {self.total_length:.2f} 像素")
        print(f"路径点数: {len(self.path_points)}")
    
    def calculate_chars_needed(self, speed_px_per_char=10):
        """
        计算在给定速度(像素/字符)下需要多少字符才能走完全程
        :param speed_px_per_char: 每个字符移动的像素数
        :return: 需要的字符数
        """
        chars_needed = math.ceil(self.total_length / speed_px_per_char)
        print(f"\n在速度 {speed_px_per_char} 像素/字符 下:")
        print(f"需要输入 {chars_needed} 个字符才能走完全程")
        print(f"实际移动距离: {chars_needed * speed_px_per_char:.2f} 像素")
        print(f"超出路径长度: {chars_needed * speed_px_per_char - self.total_length:.2f} 像素")
        return chars_needed
    
    def calculate_required_speed(self, num_chars):
        """
        计算在给定字符数下需要多快的速度才能走完全程
        :param num_chars: 计划输入的字符数
        :return: 需要的速度(像素/字符)
        """
        required_speed = self.total_length / num_chars
        print(f"\n在 {num_chars} 个字符限制下:")
        print(f"需要速度 {required_speed:.2f} 像素/字符 才能走完全程")
        print(f"实际移动距离: {num_chars * required_speed:.2f} 像素")
        return required_speed

def run_tests():
    analyzer = PathAnalyzer()
    
    # 测试用例1: 默认速度10像素/字符
    analyzer.calculate_chars_needed(speed_px_per_char=10)
    
    # 测试用例2: 自定义速度
    custom_speed = float(input("\n请输入测试速度(像素/字符，默认10): ") or 10)
    analyzer.calculate_chars_needed(custom_speed)
    
    # 测试用例3: 计算所需速度
    target_chars = int(input("\n请输入目标字符数: "))
    analyzer.calculate_required_speed(target_chars)

if __name__ == "__main__":
    run_tests()